/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
AnnotraxTime = {}

AnnotraxTime.intToPaddedString = function(i)
{
	if(i < 10)
	{
		return "0"+i;
	}
	return ""+i;
}
AnnotraxTime.secondsToTimeString = function(seconds)
{
	if(seconds < 0)
	{
		throw "illegal seconds";
	}
	h = Math.floor(seconds / 3600);
	m = Math.floor((seconds % 3600) / 60);
	s = Math.floor(seconds % 60);
		
	return AnnotraxTime.intToPaddedString(h)+":"+AnnotraxTime.intToPaddedString(m)+":"+AnnotraxTime.intToPaddedString(s);
}
AnnotraxTime.startEndToTimeString = function(start,duration)
{
	s = start;
	e = duration;
	if(typeof(start) == "string")
	{
		s = AnnotraxTime.stringToInt(start)
	}
	if(typeof(duration) == "string")
	{
		e = AnnotraxTime.stringToInt(duration)
	}
	return AnnotraxTime.secondsToTimeString(s)+"/"+AnnotraxTime.secondsToTimeString(e);
}
AnnotraxTime.stringToInt = function(str,max)	 
{
	num = parseInt(str,10)
	if(isNaN(num))
	{
		throw "illegal time format, need hh:mm:ss"
	}
	if(max != null)
	{
		if(num > max)
		{
			throw "illegal time format, need hh:mm:ss"
		}
	}
	if(num < 0)
	{
		throw "illegal time format, need hh:mm:ss"
	}
	
	return num;
	
}
AnnotraxTime.timeStringToSeconds = function(time_string)
{
	hms = time_string.split(":");
	if(hms.length != 3)
	{
		throw "illegal time format, need hh:mm:ss"
	}
	seconds = stringToInt(hms[0])*3600 + stringToInt(hms[1],59)*60 + stringToInt(hms[2],59);
	return seconds;
}